import request from '@/utils/request';
import { AxiosPromise } from 'axios';
import { AdmCarApplyVO, AdmCarApplyForm, AdmCarApplyQuery } from '@/api/general/administration/car/apply/types';
import { UploadVO, UploadForm, UploadQuery } from '@/api/general/administration/car/upload/types';

/**
 * 查询用车申请列表
 * @param query
 * @returns {*}
 */

export const listAdmCarApply = (query?: AdmCarApplyQuery): AxiosPromise<AdmCarApplyVO[]> => {
  return request({
    url: '/system/admCarApply/list',
    method: 'get',
    params: query
  });
};

/**
 * 查询用车申请详细
 * @param id
 */
export const getAdmCarApply = (id: string | number): AxiosPromise<AdmCarApplyVO> => {
  return request({
    url: '/system/admCarApply/' + id,
    method: 'get'
  });
};

/**
 * 新增用车申请
 * @param data
 */
export const addAdmCarApply = (data: AdmCarApplyForm) => {
  return request({
    url: '/system/admCarApply',
    method: 'post',
    data: data
  });
};

/**
 * 修改用车申请
 * @param data
 */
export const updateAdmCarApply = (data: AdmCarApplyForm) => {
  return request({
    url: '/system/admCarApply',
    method: 'put',
    data: data
  });
};

/**
 * 删除用车申请
 * @param id
 */
export const delAdmCarApply = (id: string | number | Array<string | number>) => {
  return request({
    url: '/system/admCarApply/' + id,
    method: 'delete'
  });
};

/**
 * 附件上传（支持多文件）
 * @param files File[] 选中的文件数组
 * @param number 业务唯一标识
 * @param type 类型（对应 remark）
 */
export const uploadAttachment = (files: File[], userId: string|number, number: string, type: number) => {
  const formData = new FormData();
  files.forEach(file => {
    formData.append("file", file); // 后端用 @RequestPart("file")
  });
  formData.append("number", number);
  formData.append("type", String(type));
  formData.append("userId", String(userId));
  return request({
    url: '/system/admCarApply/importData',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

/**
 * 删除附件
 * @param id
 */
export const deleteUploadFile = (id: string | number | Array<string | number>) => {
  return request({
    url: '/system/admCarApply/deleteUploadFile/' + id,
    method: 'delete'
  });
};


/**
 * 查询本人未审批的申请列表
 * @param query
 * @returns {*}
 */

export const queryApplyListByUser = (query?: AdmCarApplyQuery): AxiosPromise<AdmCarApplyVO[]> => {
  return request({
    url: '/system/admCarApply/queryListByUser',
    method: 'get',
    params: query
  });
};

/**
 * 删除用车申请
 * @param id
 */
export const queryAllByUser = (query?: UploadQuery): AxiosPromise<UploadVO[]> => {
  return request({
    url: '/system/admCarApply/queryAll',
    method: 'post',
    data: query
  });
};


/**
 * 查询车辆使用记录
 * @param carId 车辆ID
 * @param startTime 可选 开始时间
 * @param endTime 可选 结束时间
 */
export const getCarUseRecords = (carId: number, startTime?: string, endTime?: string, userId?: string | number) => {
  return request({
    url: '/system/admCarApply/getApplyByCar',
    method: 'get',
    params: {
      carId,
      startTime,
      endTime,
      userId
    }
  });
};